package com.ys.servers.common.verticle;

import com.ys.servers.common.data.DataReader;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import com.ys.servers.common.util.CommonUtils;

/**
 * Created by taotao on 2017/5/5.
 */
public class DeployVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {

        super.start();
        DeploymentOptions options = new DeploymentOptions().setConfig(config());
        JsonArray verticles=config().getJsonArray("verticles");

        //初始化这个vertx的reader单例
        boolean noData= config().getBoolean("noData",false);
        if(!noData) {
            DataReader.init(vertx, config());
        }

        //初始化配置里的所有verticle
        verticles.stream().forEach(o -> {
            String verticleName=((JsonObject)o).getString("name");
            vertx.deployVerticle(verticleName, options ,res->{
                if(CommonUtils.validAsyncResult(res)) {
                    System.out.println(" ============================  " + verticleName + " deploy successfully");
                }else{
                    System.out.println(" xxxxxxxxxxxxxxxxxxxxxxxxxxxx  Fail in " + verticleName + " deploy ");
                }
            });
        });

    }
}
